package com.jerome.election.server;

import com.jerome.election.http.HttpMethodEnum;
import com.jerome.election.server.paxos.PaxosMessage;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * Paxos util
 * @author Jerome
 * @date 2023/10/19
 */
public class PaxosUtil {
    /**
     * 响应 response
     * @param socket
     * @param paxosMessage
     * @throws IOException
     */
    public static void write(Socket socket, PaxosMessage paxosMessage) throws IOException {
        PrintWriter writer = new PrintWriter(socket.getOutputStream());
        writer.println(paxosMessage);
        writer.flush();
    }
    /**
     * 响应 response
     * @param writer
     * @param paxosMessage
     * @throws IOException
     */
    public static void write(PrintWriter writer, PaxosMessage paxosMessage) throws IOException {
        writer.println(paxosMessage);
        writer.flush();
    }
    /**
     * 初始化 socket 长连接处理器
     * @param socket
     * @author Jerome
     * @date 2023/10/19 16:13
     */
    public static void init(Socket socket) throws IOException {
        PrintWriter writer = new PrintWriter(socket.getOutputStream());
        writer.println(new PaxosMessage(HttpMethodEnum.PAXOS.getCode(),1,"1"));
        writer.println("Host: 127.0.0.1");
        writer.println();
        writer.flush();
    }
    /**
     * 初始化 socket 长连接处理器
     * @param writer
     * @author Jerome
     * @date 2023/10/19 16:13
     */
    public static void init(PrintWriter writer) throws IOException {
        writer.println(new PaxosMessage(HttpMethodEnum.PAXOS.getCode(),1,"1"));
        writer.println("Host: 127.0.0.1");
        writer.println();
        writer.flush();
    }
}
